package cn.study.controller;


import cn.study.entity.StudyTestPaperParticulars;
import cn.study.entity.StudyTestQuestionsOne;
import cn.study.entity.StudyTestQuestionsShortanswer;
import cn.study.service.StudyTestPaperParticularsService;
import cn.study.service.StudyTestQuestionsShortanswerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 简答试题表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-test-questions-shortanswer")
public class StudyTestQuestionsShortanswerController {
    @Autowired
    StudyTestQuestionsShortanswerService studyTestQuestionsShortanswerService;
    @Autowired
    StudyTestPaperParticularsService studyTestPaperParticularsService;
    @PostMapping("/save")
    public String save(StudyTestQuestionsShortanswer studyTestQuestionsShortanswer){
        if(studyTestQuestionsShortanswer.getTestQuestionsShortanswerAnswer().equals("")){
            studyTestQuestionsShortanswer.setTestQuestionsShortanswerAnswer("(该题没有准确答案)");
        }
        studyTestQuestionsShortanswerService.save(studyTestQuestionsShortanswer);
        return  "redirect:/study-test-questions-one/studyQuestionsList";
    }
    @PostMapping("/savee")
    public String savee(String checkedid,Integer pageid){
        String[] oneIds=checkedid.split(",");
        List<Integer> list=new ArrayList<>();
        for(String s:oneIds){
            list.add(Integer.parseInt(s));
        }
        List<StudyTestQuestionsShortanswer> list1=studyTestQuestionsShortanswerService.listByIds(list);
        List<StudyTestPaperParticulars> list2=new ArrayList<>();
        for (StudyTestQuestionsShortanswer s:list1){
            StudyTestPaperParticulars aaa=new StudyTestPaperParticulars();
            aaa.setTestPaperNum(s.getId());
            aaa.setTestPaperOpic(s.getTestQuestionsShortanswerOpic());
            aaa.setOptionOrAnswer(s.getTestQuestionsShortanswerAnswer());
            aaa.setTestPaperScore(10);
            aaa.setTestType(3);
            aaa.setTestPaperId(pageid);
            list2.add(aaa);
        }
        studyTestPaperParticularsService.saveBatch(list2);
        return "redirect:/study-test-paper/shijuanlist";
    }

    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyTestQuestionsShortanswerService.removeById(id);

        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-test-questions-one/studyQuestionsList";
    }
    @GetMapping("/upd")
    public String upd(Integer id, Model model)
    {
        StudyTestQuestionsShortanswer list=studyTestQuestionsShortanswerService.getById(id);
        model.addAttribute("list",list);


        return "study_TestQuestionOne_upd";
    }

    @PostMapping("/shoupd")
    public String roleupd(Model model, StudyTestQuestionsShortanswer sho)
    {
        studyTestQuestionsShortanswerService.updateById(sho);

        return "redirect:/study-test-questions-one/studyQuestionsList";
    }
}

